package com.gzhryc.shared_device.oem.micro.queue.times;

import java.util.Date;
import java.util.List;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.shared_device.base.enums.EOperatorType;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.cdcz.code.dao.db.CDCZOrder;
import com.gzhryc.shared_device.cdcz.code.services.CDCZOrderService;
import com.gzhryc.shared_device.common.BusyThreadPool;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.code.member.dao.db.MemberBusinessOrder;
import com.gzhryc.shared_device.oem.code.member.dao.enums.MemberBusinessOrderEnum;
import com.gzhryc.shared_device.oem.code.member.services.MemberBusinessOrderService;
import com.gzhryc.shared_device.oem.code.member.services.dto.MemberBusinessOrderSearch;
import com.gzhryc.shared_device.oem.micro.queue.CDCZOrderOperateHelper;
import com.gzhryc.shared_device.oem.micro.queue.MicroConstants;
import com.gzhryc.shared_device.oem.micro.queue.work.PCLFinishCheckRunnable;

public class Every5HourCheckJob implements Job {

    static Logger log = Logger.getLogger(Every5HourCheckJob.class);

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.info("设备订单完成检查，每5个小时检查一次");

        OperateInfo operateInfo = new OperateInfo(MicroConstants.PROGRAM_TYPE, MicroConstants.PROGRAM_TYPE_NAME);
        operateInfo.setOperator(EOperatorType.System.index(), "每5小时定时检查程序");

        BusyThreadPool.self().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    MemberBusinessOrderSearch search = new MemberBusinessOrderSearch();
                    search.setState(MemberBusinessOrderEnum.State.Working.index());
                    Integer pageNo = 1;
                    Integer pageLimit = 200;

                    OperateInfo operateInfo = new OperateInfo(MicroConstants.PROGRAM_TYPE, MicroConstants.PROGRAM_TYPE_NAME);
                    operateInfo.setOperator(EOperatorType.System.index(), "5小时定时检查程序");

                    //TODO 当年订单
                    Date currentDate = new Date();
                    List<MemberBusinessOrder> businessOrderList = MemberBusinessOrderService.self(currentDate).findBySearch(search, pageNo, pageLimit);
                    while (businessOrderList != null && businessOrderList.size() > 0) {
                        for (MemberBusinessOrder businessOrder : businessOrderList) {
                            if(EBusinessType.CDCZ.index().equals(businessOrder.getBusinessType())) {
                                CDCZOrder deviceOrder = CDCZOrderService.self(businessOrder.getCreateDate()).getByTradeNo(businessOrder.getTradeNo());
                                try {
                                	CDCZOrderOperateHelper.chekcTimeOrToFinish(deviceOrder, operateInfo);
                                }catch(Exception e) {
                                	log.error(e.getMessage(), e);
                                }
                            }
                        }
                        pageNo = pageNo + 1;
                        businessOrderList = MemberBusinessOrderService.self(currentDate).findBySearch(search, pageNo, pageLimit);
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }
        });

        BusyThreadPool.self().execute(new PCLFinishCheckRunnable(operateInfo));
    }
}
